package com.chat.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class MyHttpRequest {
    @SuppressWarnings("rawtypes")
	public static String sendGet(String url, Map<String,Object> map,String charset){
          StringBuffer sb=new StringBuffer();
          if(map!=null&&map.size()>0){
              Iterator it=map.entrySet().iterator();
              while(it.hasNext()){
                 Map.Entry  er= (Entry) it.next();
                 sb.append(er.getKey());
                 sb.append("=");
                 sb.append(er.getValue());
                 sb.append("&");
             }
          }
       return  sendGet(url,sb.toString(), charset);
    }
    public static String sendPost(String url, Map<String,Object> map,String charset){
          StringBuffer sb=new StringBuffer();
          if(map!=null&&map.size()>0){
                for (Entry<String, Object> e : map.entrySet()) {  
                    sb.append(e.getKey());  
                    sb.append("=");  
                    sb.append(e.getValue());  
                    sb.append("&");  
                }  
          }
       return  sendPost(url,sb.toString(),charset);
    }
    public static String sendGet(String url, String param,String charset) {
        String result = "";
        String line;
        StringBuffer sb=new StringBuffer();
        BufferedReader in = null;
        int iCTime = PropertiesUtil.getIntParm("http.ConnectTimeout");
        int iRTime = PropertiesUtil.getIntParm("http.ReadTimeout");
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            URLConnection conn = realUrl.openConnection();
            conn.setRequestProperty("contentType", charset); 
            conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            conn.setConnectTimeout(iCTime);
            conn.setReadTimeout(iRTime);
            conn.connect();
            in = new BufferedReader(new InputStreamReader(
                    conn.getInputStream(),charset));
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            result=sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
    public static String sendPost(String url, String param,String charset) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        String line;
        StringBuffer sb=new StringBuffer();
        int iCTime = PropertiesUtil.getIntParm("http.ConnectTimeout");
        int iRTime = PropertiesUtil.getIntParm("http.ReadTimeout");
        try {
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();
            conn.setRequestProperty("contentType", charset);  
            conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            conn.setConnectTimeout(iCTime);
            conn.setReadTimeout(iRTime);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(conn.getOutputStream());
            out.print(param);
            out.flush();
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(),charset));
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            result=sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }
}
